#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Student
{
	//数据域：值域
	int iId;
	char caName[32];
	float fScore;
	//指针域
	struct Student *pNext;
}STU;

STU *mkSTUNode()
{
	//新申请空间的原有数据不会被系统清除
	//将该数据称之为野值,使用90来覆盖掉野值
	//int a = 90;

	STU *stu = (STU*)malloc(sizeof(STU));
	if (NULL == stu)
	{//空间申请失败
		//EXIT_FAILURE 1
	    //EXIT_SUCCESS 0
		exit(EXIT_FAILURE);
	}
	else
	{//malloc申请的空间不会自动把野值覆盖掉
	 //所以需要手动对申请的空间进行垃圾数据清理	
		memset(stu, 0, sizeof(STU));	
	}
	return stu;
}

void input(STU *stu)
{
	if (NULL != stu)
	{
		printf("请输入学生的信息id,name,score:\n");
		scanf("%d%s%f", &stu->iId, stu->caName
					  , &stu->fScore);
	}
}

void display(STU *head)
{
	if (NULL == head)
	{
		return;
	}
	
	STU *node = head;
	while (NULL != node)
	{
		printf("id:%d,name:%s,score:%.1f\n", node->iId
			   , node->caName, node->fScore);
		node = node->pNext;
	}
}
#if 0
STU* reverse(STU *head)
{
	if (NULL == head)
	{
		return;
	}
	STU *p = head;
	STU *q = p->pNext;
	while (NULL != q)
	{
		p->pNext = q->pNext;
		q->pNext = head;
		head = q;
	
		q = p->pNext;
	}
	return head;
}
#endif

void reverse(STU **head)
{
	if (NULL == head)
	{
		return;
	}
	STU *p = *head;
	STU *q = p->pNext;
	while (NULL != q)
	{
		p->pNext = q->pNext;
		q->pNext = *head;
		*head = q;
	
		q = p->pNext;
	}
}

int main(void)
{
	STU *head = NULL;
	STU *node = NULL;
	int i = 0;
	for (; i<5; i++)
	{
		node = mkSTUNode();
		input(node);
		//头插法
		if (NULL == head)
		{//若head为null，则表示没有数据节点
		 //直接将node给head，作为第一个数据节点
			head = node;
		}
		else
		{//头插法，原来的第一个数据节点作为新节点的下一个
		 //新节点作为第一个数据节点
			node->pNext = head;
			head = node;
		}
	}
	display(head);
	
	//head = reverse(head);
	reverse(&head);

	display(head);

	return 0;
}



